package com.gzhryc.shared_device.oem.code;

import com.gzhryc.common.ClassTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.bxscn.api.BXSCNHttpAPINetworkService;
import com.gzhryc.shared_device.bxscn.core.IBXSCNNetworkService;
import com.gzhryc.shared_device.cdb.api.CDBHttpAPINetworkService;
import com.gzhryc.shared_device.cdb.core.ICDBNetworkService;
import com.gzhryc.shared_device.iot.code.dao.db.IOTServerNode;
import com.gzhryc.shared_device.iot.code.services.IOTServerNodeService;
import com.gzhryc.shared_device.iot.core.IOTNodeEntity;
import com.gzhryc.shared_device.cdcz.api.CDCZHttpAPINetworkService;
import com.gzhryc.shared_device.cdcz.core.ICDCZNetworkService;

import java.util.concurrent.ConcurrentHashMap;

/**
 * IOT网络服务工厂
 * 
 * @author zhanghao
 * @version 2022-09-04
 */
public class HttpNetworkServiceUtils {

	static Logger log = Logger.getLogger(HttpNetworkServiceUtils.class);

	private static ConcurrentHashMap<Long, ICDCZNetworkService> Store1 = new ConcurrentHashMap<>();

	private static ConcurrentHashMap<Long, ICDBNetworkService> Store2 = new ConcurrentHashMap<>();

	private static ConcurrentHashMap<Long, IBXSCNNetworkService> Store3 = new ConcurrentHashMap<>();

	public static ICDCZNetworkService getCDCZNetworkService(Long iotId) {
		ICDCZNetworkService service = Store1.get(iotId);
		if(service == null){
			IOTServerNode serverNode = IOTServerNodeService.self().getById(iotId);
			if(serverNode != null) {
				IOTNodeEntity iotNodeEntity = ClassTools.copy(new IOTNodeEntity(),serverNode);
				service = new CDCZHttpAPINetworkService(iotNodeEntity);
				Store1.put(iotId,service);
			}else{
				log.error("未找到IOT节点[{{0}}]的配置",iotId);
			}
		}
		return service;
	}

	public static ICDBNetworkService getCDBNetworkService(Long iotId) {
		ICDBNetworkService service = Store2.get(iotId);
		if(service == null){
			IOTServerNode serverNode = IOTServerNodeService.self().getById(iotId);
			if(serverNode != null) {
				IOTNodeEntity iotNodeEntity = ClassTools.copy(new IOTNodeEntity(),serverNode);
				service = new CDBHttpAPINetworkService(iotNodeEntity);
				Store2.put(iotId,service);
			}else{
				log.error("未找到IOT节点[{{0}}]的配置",iotId);
			}
		}
		return service;
	}

	public static IBXSCNNetworkService getBXSCNNetworkService(Long iotId) {
		IBXSCNNetworkService service = Store3.get(iotId);
		if(service == null){
			IOTServerNode serverNode = IOTServerNodeService.self().getById(iotId);
			if(serverNode != null) {
				IOTNodeEntity iotNodeEntity = ClassTools.copy(new IOTNodeEntity(),serverNode);
				service = new BXSCNHttpAPINetworkService(iotNodeEntity);
				Store3.put(iotId,service);
			}else{
				log.error("未找到IOT节点[{{0}}]的配置",iotId);
			}
		}
		return service;
	}
}
